#this is main program
import pygame
import random

from astar import *
from snake import *
from setup import *
from food import *

class World():
    def __init__(self):
        self.food=Food()
        self.begin=GRID[0]//2,GRID[1]//2        
        self.end=self.food.x,self.food.y
        self.path=[]
        #self.snake=Snake(self.begin,2,self.path)
                
    def process(self):
        
        
        self.end=self.food.x,self.food.y
        self.astar=Astar(self.begin,self.end)
        self.path=self.astar.search()
        if self.path != None:
            self.snake=Snake(self.begin,2,self.path)
        
    def render(self,screen):
        for i in range(len(self.path)): 
            clock.tick(10)
            screen.fill(QING)
            self.snake.move()
            self.snake.render(screen)

            self.food.render(screen)
            pygame.display.update()

    def grow(self):
        self.path=[]
        
        self.begin=self.food.x,self.food.y
        self.food.next_point()

pygame.init()
screen=pygame.display.set_mode(SCREEN)
pygame.display.set_caption("AutoSnake")
clock=pygame.time.Clock()

world=World()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    
        
    world.process()
    world.render(screen)
    world.grow()

    pygame.display.update()